package com.example.administrator.gaodemap;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MovePathView1 extends View {
    private final Context context;
    private Path mPath, mCirclePath;
    private Paint mPaint;
    private float[] mCurrentPosition = new float[2];
    //0-255
    private int alpha;
    private int radius;
    private boolean readyDrawCircle;
    private int bg_circle, bg_line;

    public MovePathView1(Context context) {
        this(context, null);
    }

    public MovePathView1(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MovePathView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs);
    }

    //初始化
    private void init(AttributeSet attrs) {
        initAttrs(attrs);
        //绘制直线
        mPaint = new Paint();
        mPath = new Path();
        mPaint.setColor(getResources().getColor(bg_line));
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mCirclePath = new Path();
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MovePathView1);
        if (null != attributes) {
            alpha = attributes.getInt(R.styleable.MovePathView1_circle_alpha, 100);
            radius = attributes.getInt(R.styleable.MovePathView1_circle_radius, 100);
            bg_circle = attributes.getResourceId(R.styleable.MovePathView1_bg_circle, R.color.bg_circle);
            bg_line = attributes.getResourceId(R.styleable.MovePathView1_bg_line, R.color.bg_line);
        }
        attributes.recycle();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //手指按下的位置
                float startX = event.getX();
                float startY = event.getY();
                //设置原点
                mPath.moveTo(startX, startY);
                break;
            case MotionEvent.ACTION_UP:
                drawCirle();
                break;
            case MotionEvent.ACTION_MOVE:
                float currX = event.getX();
                float currY = event.getY();
                //连线
                mPath.lineTo(currX, currY);
                invalidate();
                break;

        }
        return true;
    }

    private void drawCirle() {
        PathMeasure pathMeasure = new PathMeasure(mPath, false);
        float length = pathMeasure.getLength();
        float distance = 0;
        while (true) {
            if (distance < length) {
                pathMeasure.getPosTan(distance, mCurrentPosition, null);
                mCirclePath.addCircle(mCurrentPosition[0], mCurrentPosition[1], Constanst.PIXEL, Path.Direction.CW);
                distance += Constanst.PIXEL >= 10 ? (Constanst.PIXEL / 2 >= 25 ? 25 : Constanst.PIXEL / 2) : Constanst.PIXEL;
            } else {
                mPaint.setStyle(Paint.Style.FILL);
                mPaint.setColor(getResources().getColor(bg_circle));
                mPaint.setAlpha(alpha);
                readyDrawCircle = true;
                invalidate();
                break;
            }
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (readyDrawCircle) {
            canvas.drawPath(mCirclePath, mPaint);
        } else {
            canvas.drawPath(mPath, mPaint);
        }
    }

    //对外提供的方法，重新绘制
    public void reset() {
        readyDrawCircle = false;
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(getResources().getColor(bg_line));
        mPath.reset();
        mCirclePath.reset();
        invalidate();
    }


    public void setAlpha(int alpha) {
        this.alpha = alpha;
    }

}
